package com.rj.bd.manager.controller;


import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.codec.digest.DigestUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rj.bd.user.entity.User;
import com.rj.bd.user.service.IUserService;
import com.rj.bd.manager.entity.Manager;
import com.rj.bd.manager.service.IManagerService;

import lombok.RequiredArgsConstructor;

@ResponseBody
@Controller
@RequestMapping("/manager")
public class ManagerController {
	@Autowired
	public IManagerService  managerService;
	
	@RequestMapping("/login")
	@CrossOrigin
	public Map<String,Object> login(HttpServletRequest request){
		 String mname = request.getParameter("mname");
		 String mpassword = request.getParameter("mpassword");
	     Map<String, Object> json=new HashMap<String, Object>();
	     if (mname==null||mname.equals("")) {
				json.put("login", "0");
			
				return json;
			}
	     if (mpassword==null||mpassword.equals("")) {
				json.put("login", "0");
				
				return json;
	     }
	     Manager manager = managerService.login(mname,mpassword);
	     if (manager==null) {
	 		json.put("login","0");
	 		
	 		return json;
	 	}
	     
		String mtoken=DigestUtils.md5Hex(mname+mpassword+request.getSession().getId());
		managerService.update(mtoken,manager.getMid());
		json.put("login","1");
		json.put("code","200");
		json.put("mtoken", mtoken);
		System.out.println("name:"+mname);
		System.out.println("mpassword:"+mpassword);
		
		return json;
		
	
	}
	
	
	

	
}
